package zry.Achievement.Platform;

import zry.Search.SearchCriteriaPlatform;
import zry.Util.DBUtil;
import Connection.*;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PlatformDAOImpl extends DAOBase implements PlatformDAO {
    private static final String PLATFORM_INSERT_SQL = "INSERT INTO platform_table(achievement_id,platform_name,platform_dept,platform_time,contribution,supporting) VALUES(?,?,?,?,?,?)";
    private static final String PLATFORM_UPDATE_SQL = "UPDATE platform_table SET platform_name=?,platform_dept=?,platform_time=?,contribution=?,supporting=? WHERE achievement_id=?";
    private static final String PLATFORM_SELECT_SQL = "SELECT * FROM platform_table WHERE achievement_id=?";
    private static final String PLATFORM_SELECT_ALL_SQL = "SELECT * FROM platform_table";

    @Override
    public boolean addPlatform(Platform platform) {
        Connection con = null;
        try {
            con = getConnection();
            PreparedStatement psmt = con.prepareStatement(PLATFORM_INSERT_SQL);
            psmt.setString(1, DBUtil.fixSqlFieldValue(platform.getA_id()));
            psmt.setString(2, DBUtil.fixSqlFieldValue(platform.getP_name()));
            psmt.setString(3, DBUtil.fixSqlFieldValue(platform.getP_dept()));
            psmt.setDate(4, platform.getP_time());
            psmt.setString(5, DBUtil.fixSqlFieldValue(platform.getP_contribution()));
            psmt.setString(6, DBUtil.fixSqlFieldValue(platform.getP_supporting()));
            psmt.executeUpdate();
            psmt.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    @Override
    public boolean updatePlatform(Platform platform) {
        Connection con = null;
        try {
            con = getConnection();
            PreparedStatement psmt = con.prepareStatement(PLATFORM_UPDATE_SQL);
            psmt.setString(1, DBUtil.fixSqlFieldValue(platform.getP_name()));
            psmt.setString(2, DBUtil.fixSqlFieldValue(platform.getP_dept()));
            psmt.setDate(3, platform.getP_time());
            psmt.setString(4, DBUtil.fixSqlFieldValue(platform.getP_contribution()));
            psmt.setString(5, DBUtil.fixSqlFieldValue(platform.getP_supporting()));
            psmt.setString(6, DBUtil.fixSqlFieldValue(platform.getA_id()));
            psmt.executeUpdate();
            psmt.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    @Override
    public Platform getPlatform(String achievement_id) {
        Platform platform = null;
        Connection con = null;
        try {
            con = getConnection();
            PreparedStatement psmt = con.prepareStatement(PLATFORM_SELECT_SQL);
            psmt.setString(1, achievement_id);
            ResultSet rs = psmt.executeQuery();
            if (rs.next()) {
                platform = new Platform();
                platform.setA_id(rs.getString("achievement_id"));
                platform.setP_name(rs.getString("platform_name"));
                platform.setP_dept(rs.getString("platform_dept"));
                platform.setP_time(rs.getDate("platform_time"));
                platform.setP_contribution(rs.getString("contribution"));
                platform.setP_supporting(rs.getString("supporting"));
            }
            rs.close();
            psmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return platform;
    }

    public List<Platform> getAllPlatforms() {
        List<Platform> platforms = new ArrayList<>();
        Connection con = null;
        try {
            con = getConnection();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(PLATFORM_SELECT_ALL_SQL);
            while (rs.next()) {
                Platform platform = new Platform();
                platform.setA_id(rs.getString("achievement_id"));
                platform.setP_name(rs.getString("platform_name"));
                platform.setP_dept(rs.getString("platform_dept"));
                platform.setP_time(rs.getDate("platform_time"));
                platform.setP_contribution(rs.getString("contribution"));
                platform.setP_supporting(rs.getString("supporting"));
                platforms.add(platform);
            }
            rs.close();
            stmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return platforms;
    }

    @Override
    public List<Platform> findPlatforms(SearchCriteriaPlatform searchCriteriaPlatform) {
        List<Platform> platforms = new ArrayList<>();
        Connection con = null;
        try {
            // 构建SQL语句
            StringBuilder sqlBuilder = new StringBuilder("SELECT * FROM platform_table WHERE 1=1");
            if (searchCriteriaPlatform.getA_id() != null) {
                sqlBuilder.append(" AND achievement_id LIKE ?");
            }
            if (searchCriteriaPlatform.getP_name() != null) {
                sqlBuilder.append(" AND platform_name LIKE ?");
            }
            if (searchCriteriaPlatform.getP_dept() != null) {
                sqlBuilder.append(" AND platform_dept LIKE ?");
            }
            if (searchCriteriaPlatform.getP_time() != null) {
                sqlBuilder.append(" AND platform_time LIKE ?");
            }
            if (searchCriteriaPlatform.getP_contribution() != null) {
                sqlBuilder.append(" AND contribution LIKE ?");
            }
            if (searchCriteriaPlatform.getP_supporting() != null) {
                sqlBuilder.append(" AND supporting LIKE ?");
            }
            String sql = sqlBuilder.toString();

            // 执行查询
            con = getConnection();
            PreparedStatement psmt = con.prepareStatement(sql);
            int index = 1;
            if (searchCriteriaPlatform.getA_id() != null) {
                psmt.setString(index++, searchCriteriaPlatform.getA_id());
            }
            if (searchCriteriaPlatform.getP_name() != null) {
                psmt.setString(index++, searchCriteriaPlatform.getP_name());
            }
            if (searchCriteriaPlatform.getP_dept() != null) {
                psmt.setString(index++, searchCriteriaPlatform.getP_dept());
            }
            if (searchCriteriaPlatform.getP_time() != null) {
                psmt.setDate(index++, searchCriteriaPlatform.getP_time());
            }
            if (searchCriteriaPlatform.getP_contribution() != null) {
                psmt.setString(index++, searchCriteriaPlatform.getP_contribution());
            }
            if (searchCriteriaPlatform.getP_supporting() != null) {
                psmt.setString(index++, searchCriteriaPlatform.getP_supporting());
            }
            ResultSet rs = psmt.executeQuery();
            while (rs.next()) {
                String a_id = rs.getString("achievement_id");
                String p_name = rs.getString("platform_name");
                String p_dept = rs.getString("platform_dept");
                Date p_time = rs.getDate("platform_time");
                String p_contribution = rs.getString("contribution");
                String p_supporting = rs.getString("supporting");
                Platform platform = new Platform(a_id, p_name, p_dept, p_time, p_contribution, p_supporting);
                platforms.add(platform);
            }
            rs.close();
            psmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return platforms;
    }
}
